<?

// ********************************
//
//	extracts site struct for viewing
//
// ********************************

require_once E.'_lib/_const.php';

class SITEMAP {

	var $parent;
	var $path;
	var $lang;
	var $data;
	
	var $res;
	
	function SITEMAP() {

		$this->data = array();
		$this->res = 255;

	}
	
	function init($parent = 0, $path = '', $lang = LANG_DEF, $hook = 'map', $mask = 0) {

		$this->parent = $parent;	// start point of site tree
		$this->path = $path;		// result URL prefix
		$this->lang = $lang;		// page name language
		$this->hook = $hook;		// hook for outer code, can be 'map' for user sitemap or 'admin' for admin sitemap
		$this->mask = $mask;		// filter out pages which hasn't enabled all bits defined by mask
		
		$this->res = $this->fetch();
		return ! (bool) $this->res;
	}
	
	function fetch() {
	
		return ($this->data = $this->_Sitemap($this->parent, $this->path, $this->lang, $this->hook, $this->mask)) ? 0 : 1;
		
	}



	function _Sitemap($parent, $path, $lang, $hook, $mask) {
	
		$res = array();
	
	//	extract records that:
	//	1. have this parent
	//	2. are visible in Sitemap
	//	3. are accessible by current user, i.e.
	//		PT_ISUSER bit is 0 for ANONYMOUS, 0 or 1 for USER
	
		$m = $mask ? " AND ".dbAndS('attr', $mask) : "";

		if (($pages = dataset('db_list', array(
							'table' => tb('struct'),
							'index' => 'id',
							'order' => 'pos',	// !!! maybe bad order
							'where' => "(parent=$parent) $m AND
								(".dbAndL('tag', PT_MAPABLE).") AND 
								( (".dbAndI('tag', PT_ISUSER).") OR ".(user_info('priv',0) & PT_ISUSER)."!=0)") )) && sizeof($pages->data)) {

			foreach($pages->data as $id => $pg) {
			
				// if non-def lang or empty page name - extract page name from table 'list'
				if ( ($lang != LANG_DEF) || !$pg['name'] ) 
					if ($nm = dataset('db_item', tb('list'), "(id_list=".L_PAGENAMES.") AND (id_index=$id) AND (assoc='$lang')"))
						$pg['name'] = $nm->data['text'];
				
	
				// check page for hook 'map' to determine it's URL
				if ($code = code($hook, $pg['code'])) {	
					if (is_file("_code/_{$code}_{$hook}.php")) {
						$p = include "_code/_{$code}_{$hook}.php";				// outer code executed

						if (is_string($p)) {								// and returned string - a part of path
							if ($p)
								$pg['url'] = $path.$p.'/';
							else											// if returned empty string
								$pg['url'] = $path;
						} 													// !!! or it returned something else - need 2b checked
					}
				} else {
					$p = $pg['keyword'];										// no support for multiple keywords in one record
																				// due to single Page Name record per Page Id
					$pg['url'] = ($path) ? $path.$p.'/' : '/'.$p;
					
				}
				
				$temp = $pg;


				if ($l = $this->_Sitemap($id, $pg['url'], $lang, $hook, $mask)) 	// if found siblings - link them to 'data'
					$temp['data'] = $l;
				
				if ($code = code('sitemap', $pg['code'])) {				// execute hook "generated"
					if (is_file("_code/_{$code}_sitemap.php")) {			// hook must return an array structured like 'data'
						$p = include "_code/_{$code}_sitemap.php";		// and containing links for this page not defined in struct 
																			// but generated some another way
						if ($p)
							$temp['data'] = array_merge((array)$temp['data'], (array)$p);
					}
				}


				if ($pg['kind'] == PK_REDIRECT) 								// if not real page but redirect - show children
					if (isset($temp['data'][0]))
						$temp = $temp['data'][0];								// if redirect really has them.. (?)
	
				if ($temp)
				$res[] = $temp;													// another brick in the wall
			}
	
		}
		
		return (sizeof($res)) ? $res : false;
	}


}
